{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="col-md-8">
        <!-- 用户信息卡片 -->
        <div class="card mb-4">
            <div class="card-body text-center">
                <div class="mb-3">
                    <!-- 用户头像 -->
                    <img class="rounded-circle"
                         src="{{ url_for('static', filename='profile_pics/' + user.image_file) }}"
                         alt="头像"
                         style="width: 100px; height: 100px; object-fit: cover;">
                </div>
                <h3 class="card-title">{{ user.username }}</h3>
                <p class="text-muted">注册用户</p>
                <div class="row mt-4">
                    <div class="col-6">
                        <h5 class="text-primary">{{ post_count }}</h5>
                        <p class="text-muted">发表文章</p>
                    </div>
                    <div class="col-6">
                        <h5 class="text-primary">{{ user.posts|length }}</h5>
                        <p class="text-muted">总文章数</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- 用户的文章列表 -->
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">{{ user.username }} 的文章</h5>
            </div>
            <div class="card-body">
                {% if posts %}
                    {% for post in posts %}
                    <div class="card mb-3">
                        <div class="card-body">
                            <h6 class="card-title">
                                <a href="{{ url_for('post_detail', post_id=post.id) }}" class="text-decoration-none text-dark">
                                    {{ post.title }}
                                </a>
                            </h6>
                            <p class="card-text text-muted small">
                                {{ post.content[:100] }}{% if post.content|length > 100 %}...{% endif %}
                            </p>
                            <div class="d-flex justify-content-between align-items-center">
                                <small class="text-muted">
                                    {{ post.date_posted.strftime('%Y-%m-%d %H:%M') }}
                                </small>
                                {% if current_user.is_authenticated and current_user == user %}
                                <div class="btn-group btn-group-sm">
                                    <a href="{{ url_for('update_post', post_id=post.id) }}" class="btn btn-outline-primary btn-sm">
                                        编辑
                                    </a>
                                    <a href="{{ url_for('delete_post', post_id=post.id) }}" class="btn btn-outline-danger btn-sm"
                                       onclick="return confirm('确定要删除这篇文章吗？')">
                                        删除
                                    </a>
                                </div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                {% else %}
                    <div class="text-center py-4">
                        <p class="text-muted">该用户还没有发布任何文章</p>
                        {% if current_user.is_authenticated and current_user == user %}
                        <a href="{{ url_for('new_post') }}" class="btn btn-primary">发布第一篇文章</a>
                        {% endif %}
                    </div>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- 侧边栏 -->
    <div class="col-md-4">
        <div class="card sidebar-card">
            <div class="card-header">
                <h6 class="mb-0">用户信息</h6>
            </div>
            <div class="card-body">
                <p><strong>用户名:</strong> {{ user.username }}</p>
                <p><strong>邮箱:</strong> {{ user.email }}</p>
                <p><strong>文章数量:</strong> {{ post_count }} 篇</p>

                {% if current_user.is_authenticated and current_user == user %}
                <div class="d-grid gap-2 mt-3">
                    <a href="{{ url_for('new_post') }}" class="btn btn-primary">写新文章</a>
                    <a href="{{ url_for('account') }}" class="btn btn-outline-info">账户设置</a>
                    <a href="{{ url_for('reset_password') }}" class="btn btn-outline-warning">修改密码</a>
                    <a href="{{ url_for('index') }}" class="btn btn-outline-secondary">返回首页</a>
                </div>
                {% else %}
                <div class="d-grid gap-2 mt-3">
                    <a href="{{ url_for('index') }}" class="btn btn-outline-secondary">返回首页</a>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}